package com.example.test2.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.example.new6.R;
import com.example.new6.Test1Activity;
import com.example.test2.Test2Activity;
import com.example.test2.entity.HotSearchItem;
import com.example.test2.entity.HotSearchResponse;
import com.example.test2.retrofit.HotClient;
import com.example.test2.service.HotService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HotActivity extends AppCompatActivity {
    ListView listView;
    private Spinner hotType;
    List<HotSearchItem> items = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot);

        listView = findViewById(R.id.hot_list);
        hotType = findViewById(R.id.hot_type);

        fetchData();
    }

    private void fetchData() {
        HotService api = HotClient.getHotService();
        api.getHotSearch("baidu").enqueue(new retrofit2.Callback<HotSearchResponse>() {
            @Override
            public void onResponse(retrofit2.Call<HotSearchResponse> call, retrofit2.Response<HotSearchResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    items = response.body().result;
                    displayData();
                } else {
                    // 处理错误情况
                }
            }

            @Override
            public void onFailure(retrofit2.Call<HotSearchResponse> call, Throwable t) {
                // 处理请求失败情况
            }
        });

        // 设置Spinner
        String[] types = new String[]{"哔哩哔哩", "百度", "知乎", "微信", "抖音", "腾讯新闻", "历史上的今天", "今日头条", "微博"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, types);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hotType.setAdapter(adapter);

        hotType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                fetchDataFromSpinner(getPlatformType(types[position]));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing
            }
        });

        fetchDataFromSpinner("bilibili"); // 默认加载哔哩哔哩的热搜
    }

    private String getPlatformType(String friendlyName) {
        switch (friendlyName) {
            case "百度":
                return "baidu";
            case "知乎":
                return "zhihu";
            case "微信":
                return "weixin";
            case "哔哩哔哩":
                return "bilibili";
            case "抖音":
                return "douyin";
            case "腾讯新闻":
                return "news";
            case "历史上的今天":
                return "lishi";
            case "今日头条":
                return "toutiao";
            case "微博":
                return "weibo";
            default:
                return "bilibili"; // 默认值
        }
    }

    private void fetchDataFromSpinner(String type) {
        HotService api = HotClient.getHotService();
        api.getHotSearch(type.toLowerCase()).enqueue(new retrofit2.Callback<HotSearchResponse>() {
            @Override
            public void onResponse(retrofit2.Call<HotSearchResponse> call, retrofit2.Response<HotSearchResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    items = response.body().result;
                    displayData();
                } else {
                    // 处理错误情况
                }
            }

            @Override
            public void onFailure(retrofit2.Call<HotSearchResponse> call, Throwable t) {
                // 处理请求失败情况
            }
        });
    }

    private void displayData() {
        List<Map<String, String>> data = new ArrayList<>();
        for (HotSearchItem item : items) {
            Map<String, String> map = new HashMap<>();
            map.put("title", item.title);
            map.put("index", String.valueOf(item.index));
            map.put("hotValue", item.hotValue);
            data.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(
                this, data, android.R.layout.simple_list_item_2,
                new String[]{"title", "hotValue"},
                new int[]{android.R.id.text1, android.R.id.text2}
        );

        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            String url = items.get(position).link;
            // 使用Intent进行跳转
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
        });
    }
}
